package org.jeecgframework.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.web.service.UploadOrgService;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: UploadOrgService
 * @Description: 上传机构信息
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/uploadOrgController")
public class UploadOrgController extends BaseController {

	
	@Autowired
	private UploadOrgService uploadOrgService;
	
	/**
	 * 设备以及备件信息列表 页面跳转
	 * @return
	 */
	@RequestMapping(params = "uploadFile")
	public ModelAndView uploadFile(HttpServletRequest request) {
		return new ModelAndView("com/zybk/file_upload/uploadOrg");
	}
	
	
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		AjaxJson j = new AjaxJson();
		boolean flag = true;
		List<TSDepart> excelList = uploadOrgService.parseExcel(request);
		int l=1;
		for(TSDepart zyo:excelList){
			for(int i=l;i<excelList.size();i++){
				
				if (zyo.getDepartname().equals(excelList.get(i).getDepartname())
						|| zyo.getBpCode().equals(excelList.get(i).getBpCode())){
					j.setSuccess(false);
					j.setMsg("BP或者机构名称请保持唯一");
					flag = false;
					break;
				}
			}
			l++;
		}
		if(null!=excelList && flag){
			uploadOrgService.batchSave(excelList);
			j.setMsg("导入成功");
		}
		return j;	
	}
}
